<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
	<th:block th:include="include :: header('修改危险源点')" />
</head>
<body class="white-bg">
	<div class="wrapper wrapper-content animated fadeInRight ibox-content">
		<form class="form-horizontal m" id="form-hazard-edit" th:object="${hazard}">
			<input name="hazardId" type="hidden" th:field="*{hazardId}" />
			<input id="treeId" name="parentId" type="hidden" th:field="*{parentId}" />
			<input name="parentName" type="hidden" th:field="*{parentName}">
			<div class="form-group">
				<label class="col-sm-3 control-label">上级源点：</label>
				<div class="col-sm-8">
				    <div class="input-group">
						<input class="form-control" type="text" id="treeName" onclick="selectHazardTree()" readonly="true" th:value="${hazard.parentName == null ? '无' : hazard.parentName}">
					    <span class="input-group-addon"><i class="fa fa-search"></i></span>
				    </div>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label is-required">危险源点名称：</label>
				<div class="col-sm-8">
					<input class="form-control" type="text" name="hazardName" th:field="*{hazardName}" id="hazardName" required>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label is-required">显示排序：</label>
				<div class="col-sm-8">
					<input class="form-control" type="text" name="orderNum" th:field="*{orderNum}" required>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label">部门状态：</label>
				<div class="col-sm-8">
					<div class="radio-box" th:each="dict : ${@dict.getType('sys_show_hide')}">
						<input type="radio" th:id="${dict.id}" name="visible" th:value="${dict.itemValue}" th:field="*{visible}">
						<label th:for="${dict.id}" th:text="${dict.itemText}"></label>
					</div>
				</div>
			</div>
		</form>
	</div>
	<th:block th:include="include :: footer" />
	<script type="text/javascript">
		var prefix = ctx + "control/hazard";
		
		$("#form-hazard-edit").validate({
			onkeyup: false,
			rules:{
				hazardName:{
					remote: {
		                url: prefix + "/checkHazardNameUnique",
		                type: "post",
		                dataType: "json",
		                data: {
		                	"hazardId": function() {
		                        return $("#hazardId").val();
		                    },
		                    "parentId": function() {
		                        return $("input[name='parentId']").val();
		                    },
		        			"hazardName": function() {
		                        return $.common.trim($("#hazardName").val());
		                    },
							"token" :　storage.get("token")
		                },
		                dataFilter: function(data, type) {
		                	return $.validate.unique(data);
		                }
		            }
				},
				orderNum:{
					digits:true
				},
				email:{
                    email:true,
        		},
        		phone:{
        			isPhone:true,
        		},
			},
			messages: {
		        "hazardName": {
		            remote: "部门已经存在"
		        }
		    },
		    focusCleanup: true
		});
		
		function submitHandler() {
	        if ($.validate.form()) {
	        	$.operate.save(prefix + "/edit", $('#form-hazard-edit').serialize());
	        }
	    }
	
		/*源点管理-修改-选择父源点树*/
		function selectHazardTree() {
			var deptId = $("#treeId").val();
			if(deptId > 0) {
			    var options = {
					title: '部门选择',
					width: "380",
					url: prefix + "/selectHazardTree/" + $("#treeId").val(),
					callBack: doSubmit
				};
				$.modal.openOptions(options);
			} else {
        		$.modal.alertError("根源点不能选择");
        	}
		}
		
		function doSubmit(index, layero){
			var tree = layero.find("iframe")[0].contentWindow.$._tree;
			if ($.tree.notAllowLastLevel(tree)) {
	   			var body = layer.getChildFrame('body', index);
	   			$("#treeId").val(body.find('#treeId').val());
	   			$("#treeName").val(body.find('#treeName').val());
				$('input[name="parentName"]').val(body.find('#treeName').val());
	   			layer.close(index);
			}
		}
	</script>
</body>
</html>
